/**
 * 48. 旋转图像
 * https://leetcode-cn.com/problems/rotate-image/
 */

const { displayNestedArray } = require('../../libs/public');

/**
 * @param {number[][]} matrix
 * @return {void} Do not return anything, modify matrix in-place instead.
 */
function rotate(matrix) {
  const { length } = matrix;
  const newMatrix = new Array(length);
  for (let i = 0; i < length; i += 1) {
    newMatrix[i] = new Array(length);
  }
  for (let row = 0; row < length; row += 1) {
    for (let column = 0; column < length; column += 1) {
      newMatrix[column][length - row - 1] = matrix[row][column];
    }
  }
  for (let row = 0; row < length; row += 1) {
    for (let column = 0; column < length; column += 1) {
      matrix[row][column] = newMatrix[row][column];
    }
  }
}

let matrix;

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
rotate(matrix);
console.log(displayNestedArray(matrix));

matrix = [[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]];
rotate(matrix);
console.log(displayNestedArray(matrix));

matrix = [[1]];
rotate(matrix);
console.log(displayNestedArray(matrix));

matrix = [[1, 2], [3, 4]];
rotate(matrix);
console.log(displayNestedArray(matrix));
